package department.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;



public class DatabaseManager {
	private static DatabaseManager instance = new DatabaseManager();
	
	private BasicDataSource bds;
	private DatabaseManager(){
		bds = new BasicDataSource();
		bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		bds.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		bds.setUsername("scott");
		bds.setPassword("tiger");
	}
	
	public static DatabaseManager getInstance(){
		return instance;
	}
	public Connection getConnection() throws SQLException{
		return bds.getConnection();
	}
	
	public void close(Connection conn, PreparedStatement pstmt){
		
		if(pstmt != null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public void close(Connection conn, PreparedStatement pstmt, ResultSet rset){
		if(rset != null){
			try {
				rset.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(pstmt != null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
















